package com.draven.mall.dispatch;

import com.draven.mall.entity.Order;
import com.draven.mall.entity.Rider;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * 评分最高骑手优先算法实现
 * 选择评分最高的骑手进行分配
 */
@Component
public class HighestRatingDispatchAlgorithm implements DispatchAlgorithm {

    @Override
    public Rider assignRider(Order order, List<Rider> availableRiders) {
        if (availableRiders == null || availableRiders.isEmpty() || order == null) {
            return null;
        }
        
        // 找到评分最高的骑手
        Optional<Rider> highestRatedRider = availableRiders.stream()
            .max(Comparator.comparing(
                rider -> rider.getRating() != null ? rider.getRating() : 0f, 
                Comparator.nullsLast(Float::compare)
            ));
        
        return highestRatedRider.orElse(null);
    }

    @Override
    public int getType() {
        return 3; // 算法类型：3-评分最高骑手优先
    }

    @Override
    public String getName() {
        return "评分最高骑手优先";
    }
} 